<?php
defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH', CACHE_PATH . 'caches_model' . DS . 'caches_data' . DS);
pc_base::load_app_func('util', 'content');

class index {
    private $db;

    function __construct() {
        $this->db = pc_base::load_model('content_model');
        $this->_userid = param::get_cookie('_userid');
        $this->_username = param::get_cookie('_username');
        $this->_groupid = param::get_cookie('_groupid');
    }

    //首页
    public function init() {
        if (isset($_GET['siteid'])) {
            $siteid = intval($_GET['siteid']);
        } else {
            $siteid = 1;
        }
        $siteid = $GLOBALS['siteid'] = max($siteid, 1);
        define('SITEID', $siteid);
        $_userid = $this->_userid;
        $_username = $this->_username;
        $_groupid = $this->_groupid;
        //SEO
        $SEO = seo($siteid);
        $sitelist = getcache('sitelist', 'commons');
        $default_style = $sitelist[$siteid]['default_style'];
        $CATEGORYS = getcache('category_content_' . $siteid, 'commons');
        include template('content', 'index', $default_style);
    }

    //内容页
    public function show() {
        $catid = intval($_GET['catid']);
        $id = intval($_GET['id']);

        if (!$catid || !$id) showmessage(L('information_does_not_exist'), 'blank');
        $_userid = $this->_userid;
        $_username = $this->_username;
        $_groupid = $this->_groupid;

        $page = intval($_GET['page']);
        $page = max($page, 1);
        $siteids = getcache('category_content', 'commons');
        $siteid = $siteids[$catid];
        $CATEGORYS = getcache('category_content_' . $siteid, 'commons');

        if (!isset($CATEGORYS[$catid]) || $CATEGORYS[$catid]['type'] != 0) showmessage(L('information_does_not_exist'), 'blank');
        $this->category = $CAT = $CATEGORYS[$catid];
        $this->category_setting = $CAT['setting'] = string2array($this->category['setting']);
        $siteid = $GLOBALS['siteid'] = $CAT['siteid'];

        $MODEL = getcache('model', 'commons');
        $modelid = $CAT['modelid'];

        $tablename = $this->db->table_name = $this->db->db_tablepre . $MODEL[$modelid]['tablename'];
        $r = $this->db->get_one(array('id' => $id));
        if (!$r || $r['status'] != 99) {
            header("Location:" . APP_PATH);
            exit();
        }

        $this->db->table_name = $tablename . '_data';
        $r2 = $this->db->get_one(array('id' => $id));
        $rs = $r2 ? array_merge($r, $r2) : $r;

        //再次重新赋值，以数据库为准
        $catid = $CATEGORYS[$r['catid']]['catid'];
        $modelid = $CATEGORYS[$catid]['modelid'];

        require_once CACHE_MODEL_PATH . 'content_output.class.php';
        $content_output = new content_output($modelid, $catid, $CATEGORYS);
        $data = $content_output->get($rs);
        extract($data);

        //检查文章会员组权限
        if ($groupids_view && is_array($groupids_view)) {
            $_groupid = param::get_cookie('_groupid');
            $_groupid = intval($_groupid);
            if (!$_groupid) {
                $forward = urlencode(get_url());
                showmessage(L('login_website'), APP_PATH . 'index.php?m=member&c=index&a=login&forward=' . $forward);
            }
            if (!in_array($_groupid, $groupids_view)) showmessage(L('no_priv'));
        } else {
            //根据栏目访问权限判断权限
            $_priv_data = $this->_category_priv($catid);
            if ($_priv_data == '-1') {
                $forward = urlencode(get_url());
                showmessage(L('login_website'), APP_PATH . 'index.php?m=member&c=index&a=login&forward=' . $forward);
            } elseif ($_priv_data == '-2') {
                showmessage(L('no_priv'));
            }
        }
        if (module_exists('comment')) {
            $allow_comment = isset($allow_comment) ? $allow_comment : 1;
        } else {
            $allow_comment = 0;
        }
        //阅读收费 类型
        $paytype = $rs['paytype'];
        $readpoint = $rs['readpoint'];
        $allow_visitor = 1;
        if ($readpoint || $this->category_setting['defaultchargepoint']) {
            if (!$readpoint) {
                $readpoint = $this->category_setting['defaultchargepoint'];
                $paytype = $this->category_setting['paytype'];
            }

            //检查是否支付过
            $allow_visitor = self::_check_payment($catid . '_' . $id, $paytype);
            if (!$allow_visitor) {
                $http_referer = urlencode(get_url());
                $allow_visitor = sys_auth($catid . '_' . $id . '|' . $readpoint . '|' . $paytype) . '&http_referer=' . $http_referer;
            } else {
                $allow_visitor = 1;
            }
        }
        //最顶级栏目ID
        $arrparentid = explode(',', $CAT['arrparentid']);
        $top_parentid = $arrparentid[1] ? $arrparentid[1] : $catid;

        $template = $template ? $template : $CAT['setting']['show_template'];
        if (!$template) $template = 'show';
        //SEO
        $seo_keywords = '';
        if (!empty($keywords)) $seo_keywords = implode(',', $keywords);
        $SEO = seo($siteid, $catid, $title, $description, $seo_keywords);

        define('STYLE', $CAT['setting']['template_list']);
        if (isset($rs['paginationtype'])) {
            $paginationtype = $rs['paginationtype'];
            $maxcharperpage = $rs['maxcharperpage'];
        }
        $pages = $titles = '';
        if ($rs['paginationtype'] == 1) {
            //自动分页
            if ($maxcharperpage < 10) $maxcharperpage = 500;
            $contentpage = pc_base::load_app_class('contentpage');
            $content = $contentpage->get_data($content, $maxcharperpage);
        }
        if ($rs['paginationtype'] != 0) {
            //手动分页
            $CONTENT_POS = strpos($content, '[page]');
            if ($CONTENT_POS !== false) {
                $this->url = pc_base::load_app_class('url', 'content');
                $contents = array_filter(explode('[page]', $content));
                $pagenumber = count($contents);
                if (strpos($content, '[/page]') !== false && ($CONTENT_POS < 7)) {
                    $pagenumber--;
                }
                for ($i = 1; $i <= $pagenumber; $i++) {
                    $pageurls[$i] = $this->url->show($id, $i, $catid, $rs['inputtime']);
                }
                $END_POS = strpos($content, '[/page]');
                if ($END_POS !== false) {
                    if ($CONTENT_POS > 7) {
                        $content = '[page]' . $title . '[/page]' . $content;
                    }
                    if (preg_match_all("|\[page\](.*)\[/page\]|U", $content, $m, PREG_PATTERN_ORDER)) {
                        foreach ($m[1] as $k => $v) {
                            $p = $k + 1;
                            $titles[$p]['title'] = strip_tags($v);
                            $titles[$p]['url'] = $pageurls[$p][0];
                        }
                    }
                }
                //当不存在 [/page]时，则使用下面分页
                $pages = content_pages($pagenumber, $page, $pageurls);
                //判断[page]出现的位置是否在第一位
                if ($CONTENT_POS < 7) {
                    $content = $contents[$page];
                } else {
                    if ($page == 1 && !empty($titles)) {
                        $content = $title . '[/page]' . $contents[$page - 1];
                    } else {
                        $content = $contents[$page - 1];
                    }
                }
                if ($titles) {
                    list($title, $content) = explode('[/page]', $content);
                    $content = trim($content);
                    if (strpos($content, '</p>') === 0) {
                        $content = '<p>' . $content;
                    }
                    if (stripos($content, '<p>') === 0) {
                        $content = $content . '</p>';
                    }
                }
            }
        }
        $this->db->table_name = $tablename;
        //上一页
        $previous_page = $this->db->get_one("`catid` = '$catid' AND `id`<'$id' AND `status`=99", '*', 'id DESC');
        //下一页
        $next_page = $this->db->get_one("`catid`= '$catid' AND `id`>'$id' AND `status`=99", '*', 'id ASC');

        if (empty($previous_page)) {
            $previous_page = array('title' => L('first_page'), 'thumb' => IMG_PATH . 'nopic_small.gif', 'url' => 'javascript:alert(\'' . L('first_page') . '\');');
        }

        if (empty($next_page)) {
            $next_page = array('title' => L('last_page'), 'thumb' => IMG_PATH . 'nopic_small.gif', 'url' => 'javascript:alert(\'' . L('last_page') . '\');');
        }
        include template('content', $template);
    }

    public function img() {
        pc_base::load_sys_class('attachment', '', 0);
        $attachment = new attachment();

        $r = $attachment->upload('userthumb');
        if ($r) {
            $url = 'http://' . $_SERVER['HTTP_HOST'] . '/';
            $thumb = $url . '/uploadfile/' . $attachment->uploadedfiles[0]['filepath'];

        } else {
            $thumb = '';

        }
        echo '<script type="text/javascript">window.parent.aaa("' . $thumb . '");</script>';
    }

    public function section4() {
        if (!empty($_POST['type']) && isset($_POST['type'])) {

        } else {
            echo '请输入完整信息！';
            exit();
        }
       // echo $_POST['tel_rmb'];exit();
        if (strstr($_POST['type'], "0")) {
            if (is_numeric($_POST['tel_rmb']) && isset($_POST['tel_rmb']) && is_badword($_POST['tel_rmb']) == false) {

            } else {
                echo '请输入完整信息！';
                exit();
            }
        }
        if (strstr($_POST['type'], "1")) {
            if (is_numeric($_POST['rmb']) && isset($_POST['rmb']) && is_badword($_POST['rmb']) == false) {

            } else {
                echo '请输入完整信息！';
                exit();
            }
        }
        echo 1;
        exit();
    }

    public function section3() {
        //echo $_POST['con1'];exit();
        if ($_POST['con1'] == '' && $_POST['con2'] == '' && $_POST['con3'] == '') {
            echo '请输入完整信息！';
            exit();
        }
        if (isset($_POST['con1']) && is_badword($_POST['con1']) == false) {

        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (isset($_POST['con2']) && is_badword($_POST['con2']) == false) {

        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (isset($_POST['con3']) && is_badword($_POST['con3']) == false) {

        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (isset($_POST['con'])&&$_POST['con']!='例：策划总监，近10年的时间房地产营销，服务物业类型包括综合体、商业、住宅......') {

        } else {
            echo '请输入完整信息！';
            exit();
        }
        echo 1;
        exit();
    }
    public function section13() {
        if ($_POST['con'] == '') {
            echo '请输入完整信息！';
            exit();
        }
        if (isset($_POST['con'])) {

        } else {
            echo '请输入完整信息！';
            exit();
        }        
        echo 1;
        exit();
    }
    public function section2() {
        if (!empty($_POST['goodjob']) && isset($_POST['goodjob']) && is_badword($_POST['goodjob']) == false) {

        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (!empty($_POST['trade']) && isset($_POST['trade']) && is_badword($_POST['trade']) == false) {

        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (!empty($_POST['realname']) && isset($_POST['realname']) && is_badword($_POST['realname']) == false) {

        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (isset($_POST['mobile']) && preg_match("/^1\d{10}$/", $_POST['mobile'])) {

        } else {
            echo '请提供正确的手机号码！';
            exit();
        }
        if (!empty($_POST['job']) && isset($_POST['job']) && is_badword($_POST['job']) == false) {

        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (isset($_POST['tjr']) && is_badword($_POST['tjr']) == false) {

        } else {
            echo '请输入完整信息！';
            exit();
        }

        echo 1;
        exit();
    }

    public function aaa() {
        include template('content', 'index11');
    }

    public function bbb() {
        echo $_SESSION['goodjob'];
    }
    public function teacher_apply_bf() {
        include template('member', 'teacher_apply_bf');
    }
    public function do_teachers_apply() {
        /*$_POST['goodjob']=$_SESSION['goodjob'];
        $_POST['trade']=$_SESSION['trade'];
        $_POST['realname']=$_SESSION['realname'];
        $_POST['mobile']=$_SESSION['mobile'];
        $_POST['job']=$_SESSION['job'];
        $_POST['type']='1,2,0';*/
        $_POST['num'] = 3;
        $num = intval($_POST['num']);
        /*$_POST['con1']=$_SESSION['con1'];
        $_POST['con2']=$_SESSION['con2'];
        $_POST['con3']=$_SESSION['con3'];
        $_POST['con4']=$_SESSION['con4'];
        $_POST['con5']=$_SESSION['con5'];*/
        if (isset($_POST['need'])) {            
            $need = str_replace(" ", "", $_POST['need']);
        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (isset($_POST['con'])) {
            $con = str_replace(" ", "", $_POST['con']);
            //$con = $_POST['con'];
        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (is_numeric($_POST['tel_rmb']) && isset($_POST['tel_rmb']) && is_badword($_POST['tel_rmb']) == false) {
            $tel_rmb = $_POST['tel_rmb'];
        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (is_numeric($_POST['rmb']) && isset($_POST['rmb']) && is_badword($_POST['rmb']) == false) {
            $rmb = $_POST['rmb'];
        } else {
            echo '请输入完整信息！';
            exit();
        }

        if (isset($_POST['tjr']) && is_badword($_POST['tjr']) == false) {
            $tjr = $_POST['tjr'];
        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (!empty($_POST['goodjob']) && isset($_POST['goodjob']) && is_badword($_POST['goodjob']) == false) {
            $goodjob = $_POST['goodjob'];
        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (!empty($_POST['trade']) && isset($_POST['trade']) && is_badword($_POST['trade']) == false) {
            $trade = $_POST['trade'];
        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (!empty($_POST['realname']) && isset($_POST['realname']) && is_badword($_POST['realname']) == false) {
            $realname = $_POST['realname'];
        } else {
            echo '请输入完整信息！';
            exit();
        }
        if (isset($_POST['mobile']) && preg_match("/^1\d{10}$/", $_POST['mobile'])) {
            $mobile = $_POST['mobile'];
        } else {
            echo '请提供正确的手机号码！';
            exit();
        }
        if (!empty($_POST['job']) && isset($_POST['job']) && is_badword($_POST['job']) == false) {
            $job = $_POST['job'];
        } else {
            echo '请输入完整信息！';
            exit();
        }
        $cons = '';
        for ($i = 2; $i <= $num; $i++) {
            if (!empty($_POST['con' . $i]) && isset($_POST['con' . $i]) && is_badword($_POST['con' . $i]) == false) {
                if ($cons != '') {
                    $cons .= '、';
                }
                $cons .= $_POST['con' . $i];
            }
        }
        if (empty($cons)) {
            echo '请输入完整信息！';
            exit();
        }

        $thumb = $_POST['img'];

        $user_info = [
            'tjr' => $tjr,
            'img' => $thumb,
            'good' => $_POST['con1'],
            'con' => $con,
            'tel_rmb' => $tel_rmb,
            'rmb' => $rmb,
            'trade' => $trade,
            'realname' => $realname,
            'mobile' => $mobile,
            'job' => $job,
            'goodjob' => $goodjob,
            'type' => $cons,
            'ip' => ip(),
            'ymd' => date('Y-m-d', time()),
            'ymdhis' => date('Y-m-d H:i:s', time()),
            'status' => 0,
            'types' => $_POST['types'],
            'con1' => $_POST['con2'],
            'con2' => $_POST['con3'],
            'need' => $_POST['need'],
        ];
        //print_r($user_info);exit();
        $apply_teachers = pc_base::load_model('apply_teachers_model');
        $apply_id = $apply_teachers->insert($user_info, true);
        $i_can = pc_base::load_model('i_can_model');
        foreach ($cons as &$v) {
            $can = [
                'con' => $v,
                'p_id' => $apply_id,
                'ymd' => date('Y-m-d', time()),
                'ymdhis' => date('Y-m-d H:i:s', time()),
            ];
            $i_can->insert($can, true);
        }

        if ($apply_id > 0) {
            echo 1;
            exit();
        } else {
            echo '申请失败';
            exit();
        }
    }
    public function go_teachers_applys() {
        /*$_POST['goodjob']=$_SESSION['goodjob'];
        $_POST['trade']=$_SESSION['trade'];
        $_POST['realname']=$_SESSION['realname'];
        $_POST['mobile']=$_SESSION['mobile'];
        $_POST['job']=$_SESSION['job'];
        $_POST['type']='1,2,0';*/
        $_POST['num'] = 3;
        $num = intval($_POST['num']);
        /*$_POST['con1']=$_SESSION['con1'];
        $_POST['con2']=$_SESSION['con2'];
        $_POST['con3']=$_SESSION['con3'];
        $_POST['con4']=$_SESSION['con4'];
        $_POST['con5']=$_SESSION['con5'];*/
        
        if (!empty($_POST['realname']) && isset($_POST['realname']) && is_badword($_POST['realname']) == false) {
            $realname = $_POST['realname'];
        } else {
            echo 0;
            exit();
        }
        if (isset($_POST['mobile']) && preg_match("/^1\d{10}$/", $_POST['mobile'])) {
            $mobile = $_POST['mobile'];
        } else {
            echo 1;
            exit();
        }
        if (!empty($_POST['trade']) && isset($_POST['trade']) && is_badword($_POST['trade']) == false) {
            $trade = $_POST['trade'];
        } else {
            echo 2;
            exit();
        }
        
        if (!empty($_POST['goodjob']) && isset($_POST['goodjob']) && is_badword($_POST['goodjob']) == false) {
            $goodjob = $_POST['goodjob'];
        } else {
            echo 3;
            exit();
        }
        if (!empty($_POST['job']) && isset($_POST['job']) && is_badword($_POST['job']) == false) {
            $job = $_POST['job'];
        } else {
            echo 4;
            exit();
        }
        if (isset($_POST['tjr']) && is_badword($_POST['tjr']) == false) {
            $tjr = $_POST['tjr'];
        } else {
            echo 5;
            exit();
        }
        if ($_POST['con1'] == '' && $_POST['con2'] == '' && $_POST['con3'] == '') {
            echo 6;
            exit();
        }
        if (isset($_POST['con1']) && is_badword($_POST['con1']) == false) {

        } else {
            echo 6;
            exit();
        }
        if (isset($_POST['con2']) && is_badword($_POST['con2']) == false) {

        } else {
            echo 7;
            exit();
        }
        if (isset($_POST['con3']) && is_badword($_POST['con3']) == false) {

        } else {
            echo 7;
            exit();
        }

        if (isset($_POST['con'])&&$_POST['con']!='例：策划总监，近10年的时间房地产营销，服务物业类型包括综合体、商业、住宅......') {
            $con = str_replace(" ", "", $_POST['con']);
        } else {
            echo 8;
            exit();
        }
        if (is_numeric($_POST['tel_rmb']) && isset($_POST['tel_rmb']) && is_badword($_POST['tel_rmb']) == false) {
            $tel_rmb = $_POST['tel_rmb'];
        } else {
            echo 9;
            exit();
        }
        if (is_numeric($_POST['rmb']) && isset($_POST['rmb']) && is_badword($_POST['rmb']) == false) {
            $rmb = $_POST['rmb'];
        } else {
            echo 10;
            exit();
        }
        if (isset($_POST['need'])) {            
            $need = str_replace(" ", "", $_POST['need']);
        } else {
            echo 11;
            exit();
        }
        $cons = '';
        for ($i = 2; $i <= $num; $i++) {
            if (!empty($_POST['con' . $i]) && isset($_POST['con' . $i]) && is_badword($_POST['con' . $i]) == false) {
                if ($cons != '') {
                    $cons .= '、';
                }
                $cons .= $_POST['con' . $i];
            }
        }
        if (empty($cons)) {
            echo 7;
            exit();
        }

        $thumb = $_POST['img'];

        $user_info = [
            'tjr' => $tjr,
            'img' => $thumb,
            'good' => $_POST['con1'],
            'con' => $con,
            'tel_rmb' => $tel_rmb,
            'rmb' => $rmb,
            'trade' => $trade,
            'realname' => $realname,
            'mobile' => $mobile,
            'job' => $job,
            'goodjob' => $goodjob,
            'type' => $cons,
            'ip' => ip(),
            'ymd' => date('Y-m-d', time()),
            'ymdhis' => date('Y-m-d H:i:s', time()),
            'status' => 0,
            'types' => $_POST['types'],
            'con1' => $_POST['con2'],
            'con2' => $_POST['con3'],
            'need' => $_POST['need'],
        ];
        //print_r($user_info);exit();
        $apply_teachers = pc_base::load_model('apply_teachers_model');
        $apply_id = $apply_teachers->insert($user_info, true);
        $i_can = pc_base::load_model('i_can_model');
        foreach ($cons as &$v) {
            $can = [
                'con' => $v,
                'p_id' => $apply_id,
                'ymd' => date('Y-m-d', time()),
                'ymdhis' => date('Y-m-d H:i:s', time()),
            ];
            $i_can->insert($can, true);
        }

        if ($apply_id > 0) {
            echo 100;
            exit();
        } else {
            echo '申请失败';
            exit();
        }
    }
    public function friend() {
        $code = $_GET['code'];
        $member_model = pc_base::load_model('member_model');
        $uid = code2uid($code);

        $info = $member_model->get_one('userid=' . $uid);

        $code = uid2code($info['userid']);
        $friend = $info['friend'] ? uid2code($info['friend']) : '';

        $db_jifen_log = pc_base::load_model('jifen_log_model');
        $arr = $db_jifen_log->select(['uid' => $info['userid'], 'type' => 14], '*', 50, 'ymdhis desc');

        $all = 0;
        foreach ($arr as &$one) {
            $all += $one['price'];
            preg_match('/\d+/', $one['content'], $uuid);
            $one['uuid'] = $uuid[0];
        }

        //$yq_url = APP_PATH . "?m=act&c=a1212&code=$code";
        $yq_url = APP_PATH . "r/$code";

        $uid = (int)param::get_cookie('_userid');

        $uid = code2uid($code);
        $db = pc_base::load_model('member_model');
        $info = $db->get_one(['userid' => $uid]);

        setcookie('code', $code);

        include template('act', 'a1212');
    }

    //列表页
    public function lists() {
        $catid = $_GET['catid'] = intval($_GET['catid']);
        if ($catid==11) {
            include template('content', 'pc');exit();
        }
        $_priv_data = $this->_category_priv($catid);
        if ($_priv_data == '-1') {
            $forward = urlencode(get_url());
            showmessage(L('login_website'), APP_PATH . 'index.php?m=member&c=index&a=login&forward=' . $forward);
        } elseif ($_priv_data == '-2') {
            showmessage(L('no_priv'));
        }
        $_userid = $this->_userid;
        $_username = $this->_username;
        $_groupid = $this->_groupid;

        if (!$catid) showmessage(L('category_not_exists'), 'blank');
        $siteids = getcache('category_content', 'commons');
        $siteid = $siteids[$catid];
        $CATEGORYS = getcache('category_content_' . $siteid, 'commons');
        if (!isset($CATEGORYS[$catid])) showmessage(L('category_not_exists'), 'blank');
        $CAT = $CATEGORYS[$catid];
        $siteid = $GLOBALS['siteid'] = $CAT['siteid'];
        extract($CAT);
        $setting = string2array($setting);
        //SEO
        if (!$setting['meta_title']) $setting['meta_title'] = $catname;
        $SEO = seo($siteid, '', $setting['meta_title'], $setting['meta_description'], $setting['meta_keywords']);
        define('STYLE', $setting['template_list']);
        $page = intval($_GET['page']);

        $template = $setting['category_template'] ? $setting['category_template'] : 'category';
        $template_list = $setting['list_template'] ? $setting['list_template'] : 'list';

        if ($type == 0) {
            $template = $child ? $template : $template_list;
            $arrparentid = explode(',', $arrparentid);
            $top_parentid = $arrparentid[1] ? $arrparentid[1] : $catid;
            $array_child = array();
            $self_array = explode(',', $arrchildid);
            //获取一级栏目ids
            foreach ($self_array as $arr) {
                if ($arr != $catid && $CATEGORYS[$arr][parentid] == $catid) {
                    $array_child[] = $arr;
                }
            }
            $arrchildid = implode(',', $array_child);
            //URL规则
            $urlrules = getcache('urlrules', 'commons');
            $urlrules = str_replace('|', '~', $urlrules[$category_ruleid]);
            $tmp_urls = explode('~', $urlrules);
            $tmp_urls = isset($tmp_urls[1]) ? $tmp_urls[1] : $tmp_urls[0];
            preg_match_all('/{\$([a-z0-9_]+)}/i', $tmp_urls, $_urls);
            if (!empty($_urls[1])) {
                foreach ($_urls[1] as $_v) {
                    $GLOBALS['URL_ARRAY'][$_v] = $_GET[$_v];
                }
            }
            define('URLRULE', $urlrules);
            $GLOBALS['URL_ARRAY']['categorydir'] = $categorydir;
            $GLOBALS['URL_ARRAY']['catdir'] = $catdir;
            $GLOBALS['URL_ARRAY']['catid'] = $catid;
            include template('content', $template);
        } else {
            //单网页
            $this->page_db = pc_base::load_model('page_model');
            $r = $this->page_db->get_one(array('catid' => $catid));
            if ($r) extract($r);
            $template = $setting['page_template'] ? $setting['page_template'] : 'page';
            $arrchild_arr = $CATEGORYS[$parentid]['arrchildid'];
            if ($arrchild_arr == '') $arrchild_arr = $CATEGORYS[$catid]['arrchildid'];
            $arrchild_arr = explode(',', $arrchild_arr);
            array_shift($arrchild_arr);
            $keywords = $keywords ? $keywords : $setting['meta_keywords'];
            $SEO = seo($siteid, 0, $title, $setting['meta_description'], $keywords);
            include template('content', $template);
        }
    }

    //JSON 输出
    public function json_list() {
        if ($_GET['type'] == 'keyword' && $_GET['modelid'] && $_GET['keywords']) {
            //根据关键字搜索
            $modelid = intval($_GET['modelid']);
            $id = intval($_GET['id']);

            $MODEL = getcache('model', 'commons');
            if (isset($MODEL[$modelid])) {
                $keywords = safe_replace(new_html_special_chars($_GET['keywords']));
                $keywords = addslashes(iconv('utf-8', 'gbk', $keywords));
                $this->db->set_model($modelid);
                $result = $this->db->select("keywords LIKE '%$keywords%'", 'id,title,url', 10);
                if (!empty($result)) {
                    $data = array();
                    foreach ($result as $rs) {
                        if ($rs['id'] == $id) continue;
                        if (CHARSET == 'gbk') {
                            foreach ($rs as $key => $r) {
                                $rs[$key] = iconv('gbk', 'utf-8', $r);
                            }
                        }
                        $data[] = $rs;
                    }
                    if (count($data) == 0) exit('0');
                    echo json_encode($data);
                } else {
                    //没有数据
                    exit('0');
                }
            }
        }
    }

    /**
     * 检查支付状态
     */
    protected function _check_payment($flag, $paytype) {
        $_userid = $this->_userid;
        $_username = $this->_username;
        if (!$_userid) return false;
        pc_base::load_app_class('spend', 'pay', 0);
        $setting = $this->category_setting;
        $repeatchargedays = intval($setting['repeatchargedays']);
        if ($repeatchargedays) {
            $fromtime = SYS_TIME - 86400 * $repeatchargedays;
            $r = spend::spend_time($_userid, $fromtime, $flag);
            if ($r['id']) return true;
        }
        return false;
    }

    /**
     * 检查阅读权限
     *
     */
    protected function _category_priv($catid) {
        $catid = intval($catid);
        if (!$catid) return '-2';
        $_groupid = $this->_groupid;
        $_groupid = intval($_groupid);
        if ($_groupid == 0) $_groupid = 8;
        $this->category_priv_db = pc_base::load_model('category_priv_model');
        $result = $this->category_priv_db->select(array('catid' => $catid, 'is_admin' => 0, 'action' => 'visit'));
        if ($result) {
            if (!$_groupid) return '-1';
            foreach ($result as $r) {
                if ($r['roleid'] == $_groupid) return '1';
            }
            return '-2';
        } else {
            return '1';
        }
    }
}